package com.glory.good_dark.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.CheckModelListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.CheckModelListEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * FileName: DailyInspectionActivity
 * Author: XJH
 * Date: 2020/3/31 14:07
 * Description:
 * History:
 */
public class DailyInspectionActivity extends BaseProjectActivity {
    @BindView(R.id.rvCheckList)
    RecyclerView rvCheckList;
    @BindView(R.id.rvCheck2List)
    RecyclerView rvCheck2List;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private CheckModelListAdapter checkModelListAdapter;
    private CheckModelListAdapter checkModelListAdapter2;
    private CheckModelListEntity checkModelListEntity;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_dailyinspection;
    }

    @Override
    protected void initView() {
        initGridRecyclerView(rvCheck2List,this,4);
        initGridRecyclerView(rvCheckList,this,4);

        checkModelListAdapter = new CheckModelListAdapter(null);
        checkModelListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkModelListAdapter.isFirstOnly(true);
        rvCheckList.setAdapter(checkModelListAdapter);

        checkModelListAdapter2 = new CheckModelListAdapter(null);
        checkModelListAdapter2.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkModelListAdapter2.isFirstOnly(true);
        rvCheck2List.setAdapter(checkModelListAdapter2);

        checkModelListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                dialogs = FreeApi_DialogUtils.getScanDialog(DailyInspectionActivity.this, "请扫描或输入设备号", new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String str) {
                        if (str.contains("(R)")){
                            str = str.replace("(R)", "");
                        }

                        Log.e("replace", "print=" + str);
                        App.EQPID = str;
                        FreeApi_SharePreferencesUtils.setSharePre("GOODARK_FILE", "EQP_ID", str, App.app);
                        String name = checkModelListEntity.getINSPECTION_LIST().get(position).getField_name();
                        Bundle bundle = new Bundle();
                        bundle.putString("name",name);
                        bundle.putString("type","点检");
                        FreeApi_Utils.skipWithExtraIntent("header",bundle, DailyInspectionActivity.this, CheckDescListActivity.class);
                        dialogs.dismiss();
                    }
                });
                dialogs.show(getSupportFragmentManager(), "WEB_URL_DIALOG");
            }
        });
        checkModelListAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                dialogs = FreeApi_DialogUtils.getScanDialog(DailyInspectionActivity.this, "请扫描或输入设备号", new FreeUI_TextCallback() {
                    @Override
                    public void onSuccess(String str) {
                        if (str.contains("(R)")){
                            str = str.replace("(R)", "");
                        }

                        Log.e("replace", "print=" + str);
                        App.EQPID = str;
                        FreeApi_SharePreferencesUtils.setSharePre("GOODARK_FILE", "EQP_ID", str, App.app);
                        String name = checkModelListEntity.getSPOTCHECK_LIST().get(position).getField_name();
                        Bundle bundle = new Bundle();
                        bundle.putString("name",name);
                        bundle.putString("type","巡检");
                        FreeApi_Utils.skipWithExtraIntent("header",bundle, DailyInspectionActivity.this, CheckDescListActivity.class);
                        dialogs.dismiss();
                    }
                });
                dialogs.show(getSupportFragmentManager(), "WEB_URL_DIALOG");
            }
        });

    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDAGetCheckTypeInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"INSPECTION_LIST\":[\n" +
//                "                {\n" +
//                "                    \"check_id\":1761,\n" +
//                "                    \"config_type\":\"SUB_CLASS_INSPECTION\",\n" +
//                "                    \"field_name\":\"交接班检\",\n" +
//                "                    \"chinese_name\":null,\n" +
//                "                    \"description\":null,\n" +
//                "                    \"creator\":\"wuwanhai\",\n" +
//                "                    \"created_time\":\"2020-03-30T14:09:06\",\n" +
//                "                    \"updator\":null,\n" +
//                "                    \"updated_time\":null\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"check_id\":1760,\n" +
//                "                    \"config_type\":\"SUB_CLASS_INSPECTION\",\n" +
//                "                    \"field_name\":\"首检\",\n" +
//                "                    \"chinese_name\":null,\n" +
//                "                    \"description\":null,\n" +
//                "                    \"creator\":\"wuwanhai\",\n" +
//                "                    \"created_time\":\"2020-03-30T14:09:06\",\n" +
//                "                    \"updator\":null,\n" +
//                "                    \"updated_time\":null\n" +
//                "                }\n" +
//                "            ],\n" +
//                "            \"SPOTCHECK_LIST\":[\n" +
//                "                {\n" +
//                "                    \"check_id\":1762,\n" +
//                "                    \"config_type\":\"SUB_CLASS_SPOTCHECK\",\n" +
//                "                    \"field_name\":\"抽检\",\n" +
//                "                    \"chinese_name\":null,\n" +
//                "                    \"description\":null,\n" +
//                "                    \"creator\":\"wuwanhai\",\n" +
//                "                    \"created_time\":\"2020-03-30T14:09:24\",\n" +
//                "                    \"updator\":null,\n" +
//                "                    \"updated_time\":null\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"RETURN\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<CheckModelListEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<CheckModelListEntity>>() {}.getType());
//        checkModelListEntity = bean.getData().getBody();
//        checkModelListAdapter.setNewData(checkModelListEntity.getINSPECTION_LIST());
//        checkModelListAdapter2.setNewData(checkModelListEntity.getSPOTCHECK_LIST());
        List<CheckModelEntity> modelList = new ArrayList<>();
        CheckModelEntity entity = new CheckModelEntity("3","SUB_CLASS_SPOTCHECK","巡检");
        CheckModelEntity entity2 = new CheckModelEntity("4","SUB_CLASS_INSPECTION","点检");
        modelList.add(entity);
        modelList.add(entity2);
        FreeUI_EntityCallBack<BaseEntity<CheckModelListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<CheckModelListEntity>>
                (new TypeToken<BaseEntity<CheckModelListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<CheckModelListEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    checkModelListEntity = response.body().getData().getBody();
                    checkModelListAdapter.setNewData(checkModelListEntity.getINSPECTION_LIST());
                    checkModelListAdapter2.setNewData(checkModelListEntity.getSPOTCHECK_LIST());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<CheckModelListEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAGetCheckTypeInfo");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.add("CHECK_TYPE_LIST", new JsonParser().parse(new Gson().toJson(modelList)).getAsJsonArray());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAGetCheckTypeInfo", jsonObject);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("点巡检");
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }


    public class CheckModelEntity implements Serializable{
        private String CHECK_TYPE_ID;
        private String CONFIG_TYPE;
        private String CHECK_TYPE_NAME;

        public CheckModelEntity(String CHECK_TYPE_ID, String CONFIG_TYPE, String CHECK_TYPE_NAME) {
            this.CHECK_TYPE_ID = CHECK_TYPE_ID;
            this.CONFIG_TYPE = CONFIG_TYPE;
            this.CHECK_TYPE_NAME = CHECK_TYPE_NAME;
        }

        public String getCHECK_TYPE_ID() {
            return CHECK_TYPE_ID == null ? "" : CHECK_TYPE_ID;
        }

        public void setCHECK_TYPE_ID(String CHECK_TYPE_ID) {
            this.CHECK_TYPE_ID = CHECK_TYPE_ID;
        }

        public String getCONFIG_TYPE() {
            return CONFIG_TYPE == null ? "" : CONFIG_TYPE;
        }

        public void setCONFIG_TYPE(String CONFIG_TYPE) {
            this.CONFIG_TYPE = CONFIG_TYPE;
        }

        public String getCHECK_TYPE_NAME() {
            return CHECK_TYPE_NAME == null ? "" : CHECK_TYPE_NAME;
        }

        public void setCHECK_TYPE_NAME(String CHECK_TYPE_NAME) {
            this.CHECK_TYPE_NAME = CHECK_TYPE_NAME;
        }
    }

}
